package com.liuanbang.saro.sarodemo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

import com.liuanbang.saro.sarodemo.R;
/*
 * 自定义的ViewFliper，监听滑动手势,以及自动指引
 * @author jiakang
 */

public class GestureFliperView extends ViewFlipper implements OnGestureListener {

    GestureDetector gestureDetector = null;
    private Context mContext = null;
    FlipperFacousChangedListener flipperFacousChangedListener = null;

    public GestureFliperView(Context mContext) {
        super(mContext);

    }

    public GestureFliperView(Context mContext, AttributeSet attrs) {
        super(mContext, attrs);
        this.mContext = mContext;
        gestureDetector = new GestureDetector(mContext, this);
        setLongClickable(true);
        setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                return gestureDetector.onTouchEvent(event);
            }
        });
    }

    @Override
    public void startFlipping() {
        // TODO Auto-generated method stub
        super.startFlipping();
        setInAnimation(AnimationUtils.loadAnimation(mContext,
                R.anim.in_left2right));
        setOutAnimation(AnimationUtils.loadAnimation(mContext,
                R.anim.out_botom2top));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        stopFlipping();       //用户点击屏幕时，停止滑动
        setAutoStart(false);   //取消自动滑动
        return this.gestureDetector.onTouchEvent(event);   //把touch事件交给gesture处理
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return true; // 缺省值是false,在onTouchEvent后触发，如果为false，onFling将得不到down的事件即不触发
    }

    /*
     * 重写了onFling，为了判断手势，让手势滑动
     *
     * @see android.view.GestureDetector.OnGestureListener#onFling(android.view.
     * MotionEvent, android.view.MotionEvent, float, float)
     */

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        // TODO Auto-generated method stub
        if (e2.getX() - e1.getX() > 120) {    // 从左侧滑进
            setInAnimation(AnimationUtils.loadAnimation(mContext,
                    R.anim.in_left2right));     //设置进出动画
            setOutAnimation(AnimationUtils.loadAnimation(mContext,
                    R.anim.out_botom2top));
            //用户手势滑动结束，再次开始自动播放
            showPrevious();
            setAutoStart(true);
            startFlipping();

            return true;
        } else if (e2.getX() - e1.getX() < -120) {   //从右侧画出
            setOutAnimation(AnimationUtils.loadAnimation(mContext,
                    R.anim.in_left2right));         //设置进出动画
            setInAnimation(AnimationUtils.loadAnimation(mContext,
                    R.anim.out_botom2top));
            //滑动结束，再次自动播放
            showNext();
            setAutoStart(true);
            startFlipping();

            return true;
        }
        return false;
    }


    @Override
    public void showNext() {
        // TODO Auto-generated method stub
        super.showNext();
        //监听向下向下翻页
        flipperFacousChangedListener.onFliperChanged(getDisplayedChild());
    }

    @Override
    public void showPrevious() {
        // TODO Auto-generated method stub
        super.showPrevious();
        //监听向上翻页
        flipperFacousChangedListener.onFliperChanged(getDisplayedChild());
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                            float distanceY) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
    /*
     * 回调接口，用于监听viewflipper切花事件
     */

    public interface FlipperFacousChangedListener {
        public void onFliperChanged(int index);
    }

    public void setOnFacousChangedListener(FlipperFacousChangedListener flipperFacousChangedListener) {
        this.flipperFacousChangedListener = flipperFacousChangedListener;
    }

}